package cn.edu.cjw.APMFP.ui;

import cn.edu.cjw.APMFP.RoleRight.pojo.RoleRight;
import cn.edu.cjw.APMFP.Util.CacheUtil;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.user.Service.UserService;
import cn.edu.cjw.APMFP.user.Service.UserServiceImpl;
import cn.edu.cjw.APMFP.user.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;

public class LoginFrame extends JFrame {

    private JButton confirmBtn = new JButton("登陆");
    private JButton cancelBtn = new JButton("退出");

    private UserService userService = new UserServiceImpl();

    public LoginFrame(){
        setTitle("登陆");
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(400, 500);
        setVisible(true);
    }

    public void init(){
        this.setLayout(new BorderLayout());

        GridBagLayout gridBagLayout = new GridBagLayout();
        JPanel centerPanel = new JPanel(gridBagLayout);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(10, 10, 10, 10);
        constraints.ipady = 10;

        JLabel nameLabel = new JLabel("账号:");
        JTextField nameField = new JTextField(20);
        constraints.gridx = 0;
        constraints.gridy = 0;
        centerPanel.add(nameLabel, constraints);
        constraints.gridx = 1;
        centerPanel.add(nameField, constraints);


        JLabel descLabel = new JLabel("密码:");
        JPasswordField descField = new JPasswordField(20);
        constraints.gridx = 0;
        constraints.gridy = 1;
        centerPanel.add(descLabel, constraints);
        constraints.gridx = 1;
        centerPanel.add(descField, constraints);

        this.add(centerPanel, BorderLayout.CENTER);

        JPanel footerPanel = new JPanel();
        footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
        footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
        footerPanel.add(confirmBtn);
        footerPanel.add(cancelBtn);

//        nameField.setText("admin");
////        descField.setText("000000");
        confirmBtn.addActionListener(e -> {
//            String name = "1";
//            String pwd = "123456";
//            User user = new User();
//            user.setAccount(name);
//            user.setPassWord(pwd);
//            user.setGender("男");
//            user.setAge(21);
//            user.setName("wind");
//            RoleRight roleRight = new RoleRight();
//            roleRight.setRNum("1");
//            roleRight.setRName("人力资源");
//            roleRight.setRDescribe("人力资源");
//            roleRight.setRRight(new ArrayList<>(Arrays.asList("生产任务管理", "员工账号管理")));
//            user.setuRole(roleRight);
//            CacheUtil.login(user);

            String name = nameField.getText().trim();
            if(!StringUtil.isNotBlank(name)){
                JOptionPane.showMessageDialog(null, "账号不能为空");
                return;
            }
            String pwd = new String(descField.getPassword()).trim();
            if(!StringUtil.isNotBlank(pwd)){
                JOptionPane.showMessageDialog(null, "密码不能为空");
                return;
            }

            try {
                User user = userService.SearchUserByAccount(name);
                if(user == null){
                    JOptionPane.showMessageDialog(null, "账号不存在或密码错误");
                    return;
                }
                if(!StringUtil.isNotBlank(user.getAccount())){
                    JOptionPane.showMessageDialog(null, "账号不存在或密码错误");
                    return;
                }
                if(!pwd.equals(user.getPassWord())){
                    JOptionPane.showMessageDialog(null, "账号不存在或密码错误");
                    return;
                }
                if(user.isDeactivate()){
                    JOptionPane.showMessageDialog(null, "账号已停用");
                    return;
                }
                CacheUtil.login(user);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "登陆失败");
                return;
            }
            JOptionPane.showMessageDialog(null, "登陆成功");
            close();
            new MainFrame();
//            SwingUtilities.invokeLater(() -> {
//                new MainFrame();
//                close();
//            });
        });
        cancelBtn.addActionListener(e -> {
            System.exit(-1);
        });
        this.add(footerPanel, BorderLayout.SOUTH);
    }

    public void close(){
        this.dispose();
    }
}
